package cn.supermaster.management.common.utils;

import cn.supermaster.management.common.enums.WebType;
import cn.supermaster.management.common.page.PageDomain;
import cn.supermaster.management.common.page.TableDataInfo;
import cn.supermaster.management.common.page.TableSupport;
import cn.supermaster.management.common.response.WebResponse;
import cn.supermaster.management.common.utils.sql.SqlUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.common.utils
 * @ClassName: ResponseHelper
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/1/10 18:26
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.comn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
public class WebHelper {

    public final Logger logger = LoggerFactory.getLogger(WebHelper.class);

    /**
     * 设置请求分页数据
     */
    public static void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (CheckUtils.isNotNull(pageNum) && CheckUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }

    /**
     * 设置请求排序数据
     */
    public static void startOrderBy() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        if (TextUtils.isNotEmpty(pageDomain.getOrderBy())) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.orderBy(orderBy);
        }
    }

    /**
     * 获取request
     */
    public static HttpServletRequest getRequest() {
        return ServletUtils.getRequest();
    }

    /**
     * 获取response
     */
    public static HttpServletResponse getResponse() {
        return ServletUtils.getResponse();
    }

    /**
     * 获取session
     */
    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    public static WebResponse webResult(int rows) {
        return rows > 0 ? success() : error();
    }

    /**
     * 响应返回结果
     *
     * @param result 结果
     * @return 操作结果
     */
    public static WebResponse webResult(boolean result) {
        return result ? success() : error();
    }

    /**
     * 返回成功
     */
    public static WebResponse success() {
        return WebResponse.success();
    }

    /**
     * 返回失败消息
     */
    public static WebResponse error() {
        return WebResponse.failure();
    }

    /**
     * 返回成功消息
     */
    public static WebResponse success(String message) {
        return WebResponse.success(message);
    }

    /**
     * 返回失败消息
     */
    public static WebResponse error(String message) {
        return WebResponse.failure(message);
    }

    /**
     * 返回错误码消息
     */
    public static WebResponse error(WebType type, String message) {
        return new WebResponse(type, message);
    }

    /**
     * 页面跳转
     */
    public static String redirect(String url) {
        return TextUtils.format("redirect:{}", url);
    }
}